package com.yantong.service.impl;

import com.yantong.pojo.CentreTerminal;
import com.yantong.mapper.CentreTerminalMapper;
import com.yantong.service.CentreTerminalService;
import com.yantong.constant.BusinessCode;
import com.yantong.exception.BDException;
import com.yantong.utils.PageUtils;
import com.yantong.utils.ValidateUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *  商家门店终端service实现类
 *
 * @author huangshuai-728982210@qq.com
 * @date 2021/1/16 15:14
 */
@Service
public class CentreTerminalServiceImpl implements CentreTerminalService {

	@Autowired
	private CentreTerminalMapper centreTerminalMapper;

	@Override
	public int saveCentreTerminal(CentreTerminal centreTerminal) {
		if (centreTerminal == null) {
			return 0;
		}
		Long total = centreTerminalMapper.countById(centreTerminal.getId());
		if (total > 0) {
			throw new BDException(BusinessCode.CENTRE_TERMINAL_EXIST);
		}
		return centreTerminalMapper.insertSelective(centreTerminal);
	}

	@Override
	public PageUtils<CentreTerminal> listCentreTerminal(CentreTerminal centreTerminal,int offset, int limit) {
		PageUtils<CentreTerminal> pageUtils = new PageUtils<>();
		int total = centreTerminalMapper.selectByCount();
		if (total > 0) {
			pageUtils.setTotal(total);
			pageUtils.setRows(centreTerminalMapper.listByParam(centreTerminal,new RowBounds(offset, limit)));
		}
		return pageUtils;
	}

	@Override
	public int updateCentreTerminal(CentreTerminal centreTerminal) {
		if (centreTerminal == null || centreTerminal.getId() == null) {
			return 0;
		}
		return centreTerminalMapper.updateByPrimaryKeySelective(centreTerminal);
	}

	@Override
	public int removeCentreTerminal(Long id) {
		if (ValidateUtil.isEmptyObjectOrString(id)) {
			return 0;
		}
		return centreTerminalMapper.deleteByPrimaryKey(id);
	}

	@Override
	public CentreTerminal getCentreTerminalById(Long id) {
		if (id == null) {
			return null;
		}
		return centreTerminalMapper.selectByPrimaryKey(id);
	}
}
